package com.xlh.server.protocol.support.json;

import android.text.Html;
import android.view.View;
import android.widget.AdapterView;

import com.xlh.Utils.FontHandler;
import com.xlh.Utils.GsonManager;
import com.xlh.adapter.GridViewAndListViewAdapter;
import com.xlh.bean.ProtocolObject.LongBoxLB;
import com.xlh.bean.TitleBean;
import com.xlh.interf.IActivity;
import com.xlh.interf.ITakeMessage;

import net.nightwhistler.htmlspanner.HtmlSpanner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Created by mike@Q184377367 on 2018/7/2. on 2018/7/2.
 */

public class LongBoxLBServer implements ITakeMessage {

    private IActivity atv;
    private String hasType = "longBoxLB";
    private LongBoxLB protccoObj;
    public LongBoxLBServer(IActivity atv){
        this.atv = atv;


    }

    @Override
    public void takeMessage(String t,String s){

        if(!t.equals(this.hasType)) return;

        protccoObj =  GsonManager.gsmgr.jsonToAnyObject(s, LongBoxLB.class);

        if(protccoObj==null || protccoObj.getLeftButList()==null || protccoObj.getLeftButList().size()<=0) return;

        this.renderView(protccoObj);
    }

    @Override
    public boolean takeMessage(String s) {
        return false;
    }

    @Override
    public void init() {

    }

    @Override
    public void atViewLoadAfterInit() {

    }

    public void renderView(LongBoxLB longBoxLB){

        final LongBoxLBServer me = this;
        //列数 默认三列

        String[] strData = new String[]{"",""} ;
        String label,cmd;





        List<TitleBean> data = new ArrayList<TitleBean>();

        for (int i = 0; i < longBoxLB.getLeftButList().size(); i++) {

            TitleBean t = new TitleBean();

            t.setSpanned(this.atv.getActivity().getHtmlSpanner().fromHtml(longBoxLB.getLeftButList().get(i).getLabel()));

            t.setmAction(longBoxLB.getLeftButList().get(i).getCmd());
            data.add(t);
        }
        if (me.atv.getActivity().getmAlertLayout().getVisibility() == View.INVISIBLE) {
            me.atv.getActivity().getmAlertLayout().setVisibility(View.VISIBLE);
            me.atv.getActivity().getmAlertText().setText("");
        }

        //隐藏属于输入框的文字描述
        if (me.atv.getActivity().getmAlertInputBigText().getVisibility() == View.VISIBLE) {
            me.atv.getActivity().getmAlertInputBigText().setVisibility(View.GONE);
            me.atv.getActivity().getmAlertInputBigText().setText("");
        }



        if(me.atv.getActivity().getmAlertGrid().getDataAdapter().getCount()>0){
            me.atv.getActivity().getmAlertGrid().setVisibility(View.VISIBLE);
        }
        me.atv.getActivity().getmAlertList().getDataAdapter().setType(2);
        me.atv.getActivity().getmAlertList().getDataAdapter().setGridConums(longBoxLB.getButStyle().getColNumber());
        me.atv.getActivity().getmAlertList().getDataAdapter().setDatas(data).update();




        if (me.atv.getActivity().getmAlertList().getDataAdapter().getCount() > 0) {
            me.atv.getActivity().getmAlertList().setVisibility(View.VISIBLE);
        }else{
            me.atv.getActivity().getmAlertList().setVisibility(View.GONE);
        }

        me.atv.getActivity().getmAlertList().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                // TODO Auto-generated method stub

                me.atv.getActivity().getmAlertLayout().setVisibility(View.INVISIBLE);

                me.atv.getActivity().getmAlertText().setText("");



                if (me.atv.getActivity().mSocketClient != null) {
                    me.atv.getActivity().mSocketClient.sendMsg(me.atv.getActivity().getmAlertList()
                            .getDataAdapter()
                            .getItem(position)
                            .getmAction());

                }
                me.atv.getActivity().getmAlertList().getDataAdapter().clear();
                me.atv.getActivity().getmAlertGrid().getDataAdapter().clear();
                me.atv.getActivity().lvFloatRightList.setVisibility(View.VISIBLE);
                me.atv.getActivity().closeAlertBox();

            }
        });



    }






}
